package org.kust.bbs.admin.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.kust.bbs.admin.service.IRoleService;
import org.kust.bbs.common.web.JsonPage;
import org.kust.bbs.common.web.JsonResult;
import org.kust.bbs.pojo.dto.RoleAddDTO;
import org.kust.bbs.pojo.dto.RoleUpdateDTO;
import org.kust.bbs.pojo.vo.RoleVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 角色表 前端控制器
 * </p>
 */
@RestController
@RequestMapping("/ams/role")
@Api(tags = "角色模块")
public class RoleController {
    @Autowired
    private IRoleService roleService;

    /**
     * 角色列表
     */
    @ApiOperation(value = "后台查看角色列表")
    @GetMapping("/list")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "页数", name = "pageNum", required = true),
            @ApiImplicitParam(value = "条数", name = "sizeNum", required = true)
    })
    //@PreAuthorize("hasAuthority('/ams/admin/read')")
    public JsonResult<JsonPage<RoleVO>> listRoles(Integer pageNum, Integer sizeNum) {
        JsonPage<RoleVO> roles = roleService.listRoles(pageNum, sizeNum);
        return JsonResult.ok(roles);
    }

    /**
     * 根据角色名称模糊查询角色
     */
    @ApiOperation(value = "根据角色名称模糊查询角色列表")
    @GetMapping("/query")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "页数", name = "pageNum", required = true),
            @ApiImplicitParam(value = "条数", name = "sizeNum", required = true),
            @ApiImplicitParam(value = "角色名称", name = "query", required = true)
    })
    //@PreAuthorize("hasAuthority('/ams/admin/read')")
    public JsonResult<RoleVO> queryRole(String name) {
        RoleVO roles = roleService.queryRole(name);
        return JsonResult.ok(roles);
    }

    /**
     * 新增角色
     */
    @ApiOperation(value = "新增角色")
    @PostMapping("/save")
    //@PreAuthorize("hasAuthority('/ams/admin/update')")
    public JsonResult addRole(RoleAddDTO roleAddDTO) {
        roleService.addRole(roleAddDTO);
        return JsonResult.ok();
    }

    /**
     * 更新角色
     */
    @ApiOperation(value = "更新角色")
    @PostMapping("/update")
    //@PreAuthorize("hasAuthority('/ams/admin/update')")
    public JsonResult addRole(RoleUpdateDTO roleUpdateDTO) {
        roleService.updateRole(roleUpdateDTO);
        return JsonResult.ok();
    }

    /**
     * 删除角色
     */
    @ApiOperation(value = "删除角色", notes = "只能删除无关联角色")
    @PostMapping("/delete")
    //@PreAuthorize("hasAuthority('/ams/admin/delete')")
    public JsonResult deleteRole(Long id) {
        roleService.deleteRole(id);
        return JsonResult.ok();
    }
}
